package cn.thinkjoy.LawDataForExcel.excel;

import cn.thinkjoy.LawDataForExcel.bean.ApunishNoticeInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by thinkjoy on 2017/6/19.
 * 从Excel导入数据
 */
public class PoIExcelOutPut {
    private Workbook wb;
    private FileInputStream inputStream;

    public PoIExcelOutPut(String xlsPath) throws IOException {
        //根据指定的文件输入流导入Excel从而产生Workbook对象
        try {
            inputStream = new FileInputStream(xlsPath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //兼容高低版本
        if (xlsPath.endsWith("xls")){
            wb=new HSSFWorkbook(inputStream);
        }else if(xlsPath.endsWith("xlsx")){
            wb=new XSSFWorkbook(inputStream);
        }
    }

    public List<ApunishNoticeInfo> loadNoticeInfo() throws IOException{
        List temp=new ArrayList();
//        获取第一个表单
        Sheet sheet=wb.getSheetAt(0);
        for(Row r:sheet){//从第二行开始读
            if(r.getRowNum()<1){
                continue;
            }
            ApunishNoticeInfo apunishNoticeInfo=new ApunishNoticeInfo();
            apunishNoticeInfo.setPunishNumber(r.getCell(0).getStringCellValue());
            apunishNoticeInfo.setCaseName(r.getCell(1).getStringCellValue());
            apunishNoticeInfo.setPunishType(r.getCell(2).getStringCellValue());
            apunishNoticeInfo.setPunishReason(r.getCell(3).getStringCellValue());
            apunishNoticeInfo.setPtype(r.getCell(4).getStringCellValue());
            apunishNoticeInfo.setPunishBasis(r.getCell(5).getStringCellValue());
            apunishNoticeInfo.setAdminiName(r.getCell(6).getStringCellValue());
            apunishNoticeInfo.setLegalRepresent(r.getCell(7).getStringCellValue());
            apunishNoticeInfo.setIdNumber(r.getCell(8).getStringCellValue());
            apunishNoticeInfo.setCompanyCard(r.getCell(9).getStringCellValue());
            apunishNoticeInfo.setDriverIdNumber(r.getCell(10).getStringCellValue());
            apunishNoticeInfo.setLegalRepresent2(r.getCell(11).getStringCellValue());
            apunishNoticeInfo.setPunishMoney(r.getCell(12).getStringCellValue());
            apunishNoticeInfo.setEffectDate(r.getCell(13).getStringCellValue());
            apunishNoticeInfo.setEndDate(r.getCell(14).getStringCellValue());
            apunishNoticeInfo.setPunishOffice(r.getCell(15).getStringCellValue());
            temp.add(apunishNoticeInfo);
        }
        inputStream.close();
        wb.close();
        return temp;
    }

    /**
     * 导入已经经过处理的excel
     * @return
     * @throws IOException
     */
    public List<ApunishNoticeInfo> loadNoticeInfoHandle() throws IOException{
        List temp=new ArrayList();
        Sheet sheet=wb.getSheetAt(0);
        for(Row r:sheet){
            if(r.getRowNum()<1){
                continue;
            }
            ApunishNoticeInfo apunishNoticeInfo=new ApunishNoticeInfo();
            apunishNoticeInfo.setPunishNumber(r.getCell(0).getStringCellValue());
            apunishNoticeInfo.setCaseName(r.getCell(1).getStringCellValue());
            apunishNoticeInfo.setPunishType(r.getCell(2).getStringCellValue());
            apunishNoticeInfo.setPunishReason(r.getCell(3).getStringCellValue());
            apunishNoticeInfo.setPunishBasis(r.getCell(4).getStringCellValue());
            apunishNoticeInfo.setAdminiName(r.getCell(5).getStringCellValue());
            apunishNoticeInfo.setCreditnumber(r.getCell(6).getStringCellValue());
            apunishNoticeInfo.setOrganiznumber(r.getCell(7).getStringCellValue());
            apunishNoticeInfo.setRegistnumber(r.getCell(8).getStringCellValue());
            apunishNoticeInfo.setTaxnumber(r.getCell(9).getStringCellValue());
            apunishNoticeInfo.setIdNumber(r.getCell(10).getStringCellValue());
            apunishNoticeInfo.setLegalname(r.getCell(11).getStringCellValue());
            apunishNoticeInfo.setPunishMoney(r.getCell(12).getStringCellValue());
            apunishNoticeInfo.setEffectDate(r.getCell(13).getStringCellValue());
            apunishNoticeInfo.setEndDate(r.getCell(14).getStringCellValue());
            apunishNoticeInfo.setPunishOffice(r.getCell(15).getStringCellValue());
            temp.add(apunishNoticeInfo);
        }
        inputStream.close();
        wb.close();
        return temp;
    }
}
